package me.hl.collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * Created by Planck on 2016/1/30.
 */
public class CollectionDemo {

    @Test
    //复制一个list到另外一个list
    public void copy() {
        List<String> list = new ArrayList<>();
        list.add("aaaa");
        list.add("bbbb");
        List<String> list1 = new ArrayList<>();
        System.out.println(list);
        Collections.copy(list, list1);
        list.add("cccc");
        list1.forEach(System.out::println);

    }


    @Test
    //获得最大值
    public void max() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Random random = new Random();
            list.add(random.nextInt(100));
        }
        Integer max = Collections.max(list, (a, b) -> a - b);
        System.out.println(max);
    }

    @Test
    //排序
    public void sort() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Random random = new Random();
            list.add(random.nextInt(100));
        }
        //有两种方式。第一种用Collection自带的
        Collections.sort(list, (a, b) -> a - b);
        list.forEach(System.out::println);
        System.out.println();
        //第二种用JAVA8的新特性
        list = list.stream().filter((x) -> x != null).sorted((a, b) -> b - a).collect(Collectors.toList()); //这里是需要赋值的
        list.forEach(System.out::println);
    }


}
